package com.zzyl.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.exception.BusinessException;
import com.zzyl.service.BedService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoomController extends BaseController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private BedService bedService;

    //新增房间
    @PostMapping("/room/add")
    public ResponseResult addRoom(@RequestBody Room room){
        //缺少房间类型id type_id 不能调用mp的save
        roomService.saveOrUpdateRoom(room);
        return success();
    }

    //查询指定楼层下的所有房间
    @GetMapping("/room/getRoomsByFloorId/{floorId}")
    public ResponseResult getRoomsByFloorId(@PathVariable("floorId") Long floorId){
        List<RoomVo>list=roomService.getRoomsByFloorId(floorId);
        return success(list);
    }

    //查询房间详情
    @GetMapping("/room/get/{id}")
    public ResponseResult getRoom(@PathVariable("id") Long id){
        RoomVo roomVo=roomService.getRoom(id);
        return success(roomVo);
    }

    //修改房间
    @PutMapping("/room/update")
    public ResponseResult update(@RequestBody Room room){
        roomService.saveOrUpdateRoom(room);
        return success();
    }

    //删除房间
    @DeleteMapping("/room/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Long id){
        //判断房间内有没有床位 有床位则无法删除
        //创建条件选择器  查询该房间下的床位信息
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bed::getRoomId,id);

        //判断房间内有没有床位
        if (bedService.count(queryWrapper)>0){
            throw new BusinessException("房间内仍有床位,无法删除");
        }
        roomService.removeById(id);
        return success();
    }
}
